package stef.home.arp.labos1.matrix;

import java.math.BigDecimal;
import java.util.List;

public class Matrix {
	private int nrows,ncolumns;
	private double[][] matrix;
	
	public Matrix(int nrows,int ncolumns, double[][] matrix){
		this.nrows = nrows;
		this.ncolumns = ncolumns;
		this.matrix = matrix;
	}
	
	public Matrix(int nrows, int ncolumns, List<Double> elements) {
		matrix = new double[nrows][ncolumns];
		this.nrows = nrows;
		this.ncolumns = ncolumns;
		int k=0;
		for(int i = 0; i < nrows;i++)
			for(int j=0; j < ncolumns; j++)
				matrix[i][j] = elements.get(k++);
	}

	public int getNrows() {
		return nrows;
	}


	public int getNcolumns() {
		return ncolumns;
	}


	public double[][] getMatrix() {
		return matrix;
	}

	public double getElement(int row, int column){
		return matrix[row][column];
	}
	
	public void setElement(double element, int row, int column){
		matrix[row][column] = element;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(!(obj instanceof Matrix))
			return false;
		Matrix o = (Matrix) obj;
		if(ncolumns != o.ncolumns && nrows != o.nrows)
			return false;
		for(int i = 0;i<ncolumns;i++)
			for(int j=0;j<nrows;j++)
				if(matrix[j][i] != o.matrix[j][i])
					return false;
		return true;
	}
	
	public void swapRows(int i, int j){
		double[] temp = new double[ncolumns];
		for(int k = 0;k<ncolumns;k++)
			temp[k]=matrix[i][k];
		for(int k = 0;k<ncolumns;k++)
			matrix[i][k] = matrix[j][k];
		for(int k = 0;k<ncolumns;k++)
			matrix[j][k] = temp[k];
	}
	
	public Matrix getColumn(int column){
		double[][] e = new double[nrows][1];
		for(int i = 0;i<nrows;i++)
			e[i][0] = matrix[i][column];
		return new Matrix(nrows, 1, e);
	}
	
	public void setColum(int column,Matrix e){
		for(int i = 0;i<nrows;i++)
			setElement(e.getElement(i, 0), i, column);
	}
	
	public int findMax(int column,int offset){
		double max = 0;
		int index = -1;
		for(int i = offset;i<nrows;i++){
			if(Math.abs(matrix[i][column]) > max){
				max = Math.abs(matrix[i][column]);
				index = i;
			}
		}
		return index;
	}
}
